home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50a Issue 142 (CD142a) (August 1998).iso / trial / demon / TURNPIKE.1 / CLASSES.ZIP / sun / NET / WWW / PROTOCOL / HTTP / HttpURLConnection.class (.txt) < prev   
Encoding:
Java Class File  |  1997-04-14  |  4.9 KB  |  193 lines

  1. package sun.net.www.protocol.http;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.io.PrintStream;
  7. import java.net.URL;
  8. import sun.net.ProgressData;
  9. import sun.net.www.MessageHeader;
  10. import sun.net.www.MeteredStream;
  11. import sun.net.www.URLConnection;
  12. import sun.net.www.http.AuthenticationInfo;
  13. import sun.net.www.http.HttpClient;
  14.  
  15. public class HttpURLConnection extends URLConnection {
  16.    static final String EOL = "\r\n";
  17.    static final String version = System.getProperty("java.version");
  18.    public static final String userAgent;
  19.    static final String acceptString = "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n";
  20.    static final int maxRedirections = 5;
  21.    HttpClient http;
  22.    Handler handler;
  23.    InputStream inputStream;
  24.    boolean outputStreamOpen = false;
  25.  
  26.    HttpURLConnection(URL var1, Handler var2) {
  27.       super(var1);
  28.       this.handler = var2;
  29.    }
  30.  
  31.    public HttpURLConnection(URL var1, String var2, int var3) {
  32.       super(var1);
  33.       this.handler = new Handler(var2, var3);
  34.    }
  35.  
  36.    public void connect() throws IOException {
  37.       if (!super.connected) {
  38.          super.connected = true;
  39.          ProgressData.pdata.register(super.url);
  40.          this.http = null;
  41.  
  42.          try {
  43.             if (super.url.getProtocol().equals("https")) {
  44.                throw new IOException("https not supported for this version.");
  45.             }
  46.  
  47.             this.http = new HttpClient(super.url, this.handler.proxy, this.handler.proxyPort);
  48.          } catch (Throwable var2) {
  49.             if (this.http != null) {
  50.                this.http.closeServer();
  51.             }
  52.  
  53.             ProgressData.pdata.unregister(super.url);
  54.             if (var2 instanceof SecurityException) {
  55.                throw (SecurityException)var2;
  56.             }
  57.  
  58.             throw var2 instanceof IOException ? (IOException)var2 : new IOException(var2.toString());
  59.          }
  60.  
  61.          if (this.http == null) {
  62.             throw new IOException("Couldn't connect to " + super.url.toExternalForm());
  63.          }
  64.       }
  65.    }
  66.  
  67.    private static AuthenticationInfo getAuthentication(URL var0, String var1, String var2) {
  68.       return null;
  69.    }
  70.  
  71.    public OutputStream getOutputStream() throws IOException {
  72.       String var1 = ((java.net.URLConnection)this).getRequestProperty("content-type");
  73.       this.connect();
  74.       if (this.inputStream != null) {
  75.          throw new IOException("Cannot write output after reading input.");
  76.       } else if (this.outputStreamOpen) {
  77.          throw new IOException("Cannot open output twice.");
  78.       } else {
  79.          HttpClient var2 = this.http;
  80.          PrintStream var5 = var2.serverOutput;
  81.          this.outputStreamOpen = true;
  82.          PrintStream var3 = new PrintStream(var5);
  83.          String var4 = "POST " + this.http.getURLFile(super.url) + " HTTP/1.0" + "\r\n" + userAgent + "Referer: " + super.url + "\r\n" + "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n";
  84.          if (var1 == null) {
  85.             var1 = "application/x-www-form-urlencoded";
  86.          }
  87.  
  88.          var4 = var4 + "Content-type: " + var1 + "\r\n";
  89.          var3.print(var4);
  90.          return new HttpPostBufferStream(var5);
  91.       }
  92.    }
  93.  
  94.    public InputStream getInputStream() throws IOException {
  95.       if (this.inputStream != null) {
  96.          return this.inputStream;
  97.       } else {
  98.          int var2 = 0;
  99.  
  100.          while(true) {
  101.             this.connect();
  102.             if (!this.outputStreamOpen) {
  103.                String var3 = "GET " + this.http.getURLFile(super.url) + " HTTP/1.0" + "\r\n" + userAgent + "" + "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n" + "\r\n";
  104.                Runtime var4 = Runtime.getRuntime();
  105.                HttpClient var5 = this.http;
  106.                PrintStream var15 = new PrintStream(var4.getLocalizedOutputStream(var5.serverOutput));
  107.                var15.print(var3);
  108.                var15.flush();
  109.             }
  110.  
  111.             try {
  112.                this.http.processRequest(super.url.getFile());
  113.                HttpClient var7 = this.http;
  114.                Object var1 = var7.serverInput;
  115.                var7 = this.http;
  116.                String var9 = var7.mimeHeader != null ? var7.mimeHeader.findValue("location") : null;
  117.                if (var9 != null) {
  118.                   if (this.outputStreamOpen) {
  119.                      this.http.closeServer();
  120.                      super.connected = false;
  121.                      this.http = null;
  122.                      throw new IOException("Cannot redirect with POST: " + super.url);
  123.                   }
  124.  
  125.                   ++var2;
  126.                   if (var2 > 5) {
  127.                      throw new IOException("Too many redirections (" + var2 + "): " + super.url);
  128.                   }
  129.  
  130.                   ProgressData.pdata.unregister(super.url);
  131.                   URL var10 = new URL(super.url, var9);
  132.                   super.url = var10;
  133.                   if (!"http".equals(super.url.getProtocol())) {
  134.                      this.http.closeServer();
  135.                      super.connected = false;
  136.                      this.http = null;
  137.                      throw new IOException("Cannot redirect to other protocol: " + super.url);
  138.                   }
  139.  
  140.                   ProgressData.pdata.register(super.url);
  141.                }
  142.  
  143.                HttpClient var11 = this.http;
  144.                if (var11.status == 200 || var9 == null) {
  145.                   var11 = this.http;
  146.                   ((URLConnection)this).setProperties(var11.mimeHeader);
  147.                   if (((URLConnection)this).getProperties() == null) {
  148.                      ((URLConnection)this).setProperties(new MessageHeader());
  149.                   }
  150.  
  151.                   var11 = this.http;
  152.                   String var14 = var11.mimeHeader != null ? var11.mimeHeader.findValue("content-length") : null;
  153.                   int var16 = 0;
  154.                   if (var14 != null && (var16 = Integer.parseInt(var14)) != 0) {
  155.                      var1 = new MeteredStream((InputStream)var1, var16, super.url);
  156.                   } else {
  157.                      ProgressData.pdata.unregister(super.url);
  158.                   }
  159.  
  160.                   this.inputStream = (InputStream)var1;
  161.                   return (InputStream)var1;
  162.                }
  163.  
  164.                this.http.closeServer();
  165.                ProgressData.pdata.unregister(super.url);
  166.                this.http = null;
  167.                this.inputStream = null;
  168.                super.connected = false;
  169.             } catch (IOException var6) {
  170.                ProgressData.pdata.unregister(super.url);
  171.                throw var6;
  172.             }
  173.          }
  174.       }
  175.    }
  176.  
  177.    public String getHeaderField(String var1) {
  178.       if (this.http == null) {
  179.          try {
  180.             this.getInputStream();
  181.          } catch (Exception var3) {
  182.          }
  183.       }
  184.  
  185.       HttpClient var2 = this.http;
  186.       return var2.mimeHeader != null ? var2.mimeHeader.findValue(var1) : null;
  187.    }
  188.  
  189.    static {
  190.       userAgent = "User-Agent: " + System.getProperty("http.agent", "Java" + version) + "\r\n";
  191.    }
  192. }
  193.